#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

 struct ListNode {
      int val;
      struct ListNode *next;
  };


  struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(head==NULL||head->next==NULL)
        return head;
    
    struct ListNode* prev = NULL;
    struct ListNode* cur = head;
    struct ListNode* next = cur->next;

    while(next)
    {
        if(cur->val!=next->val)
        {
            prev = cur;
            cur = next;
            next = next->next;
        }
        else
        {
            while(next&&cur->val==next->val)
            {
                next = next->next;
            }

            if(prev)
                prev->next = next;
            else
                head = next;
            
            while(cur!=next)
            {
                struct ListNode* tmp =cur;
                cur = cur->next;
                free(tmp);
            }
            if(cur)
                next = cur->next;
            else
                next = NULL;
        }
    }
    return head;
}